package controllers.remberme

import jp.t2v.lab.play2.auth.LoginLogout
import models.Account
import play.api.data.Form
import play.api.mvc.{Controller, Action}

import views.html

import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import play.api.data.Forms._
object Session extends Controller with LoginLogout with AuthConfigImp {

  val loginForm = Form {
    mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
      .verifying("Invalid email or password", result => result.isDefined)
  }
  val remembermeForm = Form {
    "rememberme" -> boolean
  }

  def login = Action { implicit request =>
    Ok(html.remberme.login(loginForm, remembermeForm.fill(request.session.get("rememberme").exists("true" ==))))
  }

  def logout = Action.async { implicit request =>
    gotoLogoutSucceeded.map(_.flashing(
      "success" -> "You've been logged out"
    ))
  }

  def authenticate = Action.async { implicit request =>
    val rememberme = remembermeForm.bindFromRequest()
    loginForm.bindFromRequest.fold(
    formWithErrors => Future.successful(BadRequest(html.remberme.login(formWithErrors, rememberme))),
    { user =>
      val req = request.copy(tags = request.tags + ("rememberme" -> rememberme.get.toString))
      gotoLoginSucceeded(user.get.id)(req, defaultContext).map(_.withSession("rememberme" -> rememberme.get.toString))
    }
    )
  }

}
